<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog :title="!dataForm.id ? '批量修改' : '批量修改'" :close-on-click-modal="false" :visible.sync="visible">
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">

      <el-form-item label="批次名称" prop="batchName">
        <el-input v-model="dataForm.batchName" placeholder="批次名称"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="codeType">
        <el-radio v-model="dataForm.codeType" :label="0">固定</el-radio>
        <el-radio v-model="dataForm.codeType" :label="1">随机</el-radio>
      </el-form-item>
      <el-form-item label="领取范围" prop="userLimit">
        <el-radio v-model="dataForm.userLimit" :label="0">所有</el-radio>
        <el-radio v-model="dataForm.userLimit" :label="1">经销商</el-radio>
      </el-form-item>
      <el-form-item label="积分" prop="addNumber" v-if="dataForm.codeType == 0">
        <el-input v-model="dataForm.addNumber" type="number" placeholder="积分数"></el-input>
      </el-form-item>
      <el-form-item label="最大积分" prop="maxNumber" v-if="dataForm.codeType == 1">
        <el-input-number :min="0" v-model="dataForm.maxNumber" placeholder="最大积分"></el-input-number>
      </el-form-item>
      <el-form-item label="最小积分" prop="minNumber" v-if="dataForm.codeType == 1">
        <el-input-number :min="0" v-model="dataForm.minNumber"   placeholder="最小积分"></el-input-number>
      </el-form-item>
      <el-form-item label="间隔" prop="intervalNumber" v-if="dataForm.codeType == 1">
        <el-input-number :min="0" v-model="dataForm.intervalNumber" placeholder="间隔"></el-input-number>
      </el-form-item>
      <el-form-item label="" prop="tt">
        已使用的不会修改,批次需填写完整,选择了表格的积分码批次名称将会忽略
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()" :loading="btnLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/integralCode.js'

export default {
  data() {
    return {
      visible: false,
      btnLoading: false,
      dataForm: {
        id: 0,
        integralCode: '',
        codeType: 0,
        maxNumber: 1,
        minNumber: 0,
        intervalNumber: 0,
        addNumber: undefined,
        batchName: '',
        generateNum: 1,
        used: '',
        useTime: '',
        codePath: '',
        isDel: '',
        createTime: '',
        updateTime: '',
        remark: ''
      },
      dataRule: {
        addNumber: [
          { required: true, message: '积分数量为必填项', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(ids) { // 初始化表单验证规则
      this.dataForm.ids = ids || 0
      this.visible = true

    },
    // 表单数据提交
    dataSubmit() {
      this.btnLoading = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          api.integralcodeBatchUpdateApi(this.dataForm).then(res => {
            // TODO 保存数据
            this.$emit('refreshDataList', "form")
            this.btnLoading = false
          }).catch(res => {
            this.btnLoading = false
          })
        } else {
          setTimeout(() => {
            this.btnLoading = false
          }, 500)
        }
      })
    }
  }
}
</script>
